﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mathematics.Parser
{
    public class TokenString:LinkedList<Token>
    {
        //public bool IsUnprocessed
        //{
        //    get { return this.Any(t => t.Type == TokenType.Unparsed); }
        //}

        public LinkedListNode<Token> Replace(Token oldToken, Token newToken)
        {
            var node = this.Find(oldToken);
            var previousNode = node.Previous;
            this.Remove(node);
            return (previousNode==null)
                ? AddFirst(newToken)
                : AddAfter(previousNode, newToken);
        }

        public TokenString Substring(Token startItem)
        {
            return Substring(startItem, null);
        }

        public TokenString Substring(Token startItem, Token endItem)
        {
            var substring = new TokenString();
            bool capture = false;

            foreach (var i in this)
            {
                if (i == startItem)
                    capture = true;

                if (capture)
                    substring.AddLast(i);

                if (i == endItem)
                    break;
            }
            return substring;
        }

        public void Snip(Token startItem, Token endItem)
        {
            var sub = Substring(startItem, endItem);
            foreach (var i in sub)
                Remove(i);
        }
    }
}
